# 注册模块
define (require, exports, module) ->

  # 引入样式
  require 'mods/header/header_css.css'
  # 引入auth信息
  header_api = require 'mods/header/header_api'

  # 引入获取终端类型
  {getDeviceType} = require "js/device_type"

  bus = require 'mods/bus'

  defNavTitles = [
    {'name':'首页', 'url':'/'}
    {'name':'产品', 'url':'/product'}
    {'name':'公告', 'url': "/notice_list"}
    {'name':'公司动态', 'url':'/gsdt'}
    {'name':'关于我们', 'url':'/gsjs'}
  ]

  # 定义组件
  module.exports = Vue.extend

    # 组件模板
    template: require 'mods/header/header_tpl.js'

    # 声明变量
    data: () ->
      menuClass:
        toggle: ''
        menuTucked: 'custom-menu-tucked'
      # 获取设备类型
      device: getDeviceType()

      navTitles: defNavTitles

      # 显示的菜单类型，0水平；1垂直
      menuType: 0
      # 是否显示搜索框
      isShowSearch: false
      # 当前的请求路径
      currentDisplayUrl: window.location.href
     # 是否登录
      logined: false
      # 用户名
      userName: ''
      # 弹出框提示
      dlgOpt: {}
      # 当前导航,改变菜单样式
      curNav: -1
      # 用户信息
      userInfo: {}
      # 搜索关键字
      q: ''
      # 是否显示搜索框下拉列表
      showSelectItem: false
      # 搜索出错提示
      search_down_tips: ''
      # 搜索列表
      selectItemList: []
  
    # 组件方法
    methods:

      # 弹出提示
      dialogTip: () ->
        self = @
        # 工具条提示
        dialogTipsObj =
          state: true
          title: '退出登录提示'
          content: '您确定退出登录吗?'
          msg_type: 'confirm'
          closeBt: true
          # 弹出框点击确定事件
          ok: () ->
            # 退出登录
            self.doLoginOut()
            # 隐藏弹出框
            self.dlgOpt.state = false
          # 弹出框点击取消事件
          cancel: () ->
            # 隐藏弹出框
            self.dlgOpt.state = false

        self.dlgOpt = Vue.util.extend {}, dialogTipsObj

      # 路由跳转
      toggleTab: (currPath, index) ->
        self = @
        self.curNav = index
        self.menuType = 0
        self.$router.push {path: currPath}

      # 点击用户名，切换到个人中心
      checkInfo: () ->
        self = @
        self.curNav=-1
        currPath = '/user_info'
        self.$router.push currPath

      # 刷新页面，设置当前菜单对应页面样式
      setCurNavStyle: () ->
        self = @
        curUrl = window.location.href
        if '/' is curUrl.charAt(curUrl.length - 1)
          self.curNav = 0
        else if 0 <= curUrl.indexOf('prod')
          self.curNav = 1
        else if 0 <= curUrl.indexOf('notice')
          self.curNav = 2
        else if 0 <=curUrl.indexOf('gsdt')
          self.curNav = 3
        else if 0 <= curUrl.indexOf('rczp')
          self.curNav = 4
        else
          self.curNav = -1

      # 获取用户登录信息
      getUserSession: () ->
        self = @

        oStr = sessionStorage.getItem 'userInfo'

        if not oStr
          self.logined = false
          return

        self.userInfo = JSON.parse oStr
        self.userName = self.userInfo?.name

        self.setCurNavStyle()
        self.logined = true

      # 退出登录
      doLoginOut: () ->
        self =  @
        header_api.doLoginOut()
          .then (res) ->
            if 200 isnt res?.status
              self.logined = true
            else
              self.logined = false
              self.curNav = 0
              sessionStorage.removeItem 'userInfo'
              self.$router.push {path:'/'}
          .catch (err) ->
            console.log 'err:',err

      # 获取用户风险等级信息
      getUserRiskInfo: () ->
        self = @

        oStr = sessionStorage.getItem 'userInfo'
        self.userInfo = JSON.parse oStr

        if not self.usserInfo?.name and self.usserInfo?.phone
          self.logined = false

        else
          self.logined = true
          self.userName = self.usserInfo?.name

      # 获取搜索列表
      getSearchList: () ->
        self = @
        self.isShowSearch = false
        sessionStorage.setItem 'q',self.q
        bus.$emit('getSearchList', self.q)
        self.$router.push {name: 'prod_search', params: {'q': self.q} }
        self.q = ''
        self.selectItemList = []

      # 搜索框键盘弹起触发事件
      getSearchDownList: () ->
        self = @

        params =
          q: self.q,
          pageNum: 1

        return header_api.getSearchList params
          .then (res) ->
            self.selectItemList = res?.data
            self.showSelectItem = true
          .catch (err) ->
            console.error 'err:',err

      # 搜索查看详情
      onSearchInfo: (url) ->
        self = @
        # 将搜索框隐藏并情况搜索信息
        self.isShowSearch = false
        self.q = ''
        self.selectItemList = []
        self.$router.push {path: url}

    # 监听路由变化
    watch:
      '$route':'setCurNavStyle'

    # 创建时触发
    created: () ->
      self = @
      bus.$on 'login_stu', (msg) ->
        self.getUserSession()

    # 路由切换时销毁events bus
    beforeRouteLeave: (to, from, next) ->
      self = @
      bus.off()
      return next true

    # 引用组件
    components:
      'c-dialog': require 'mods/dialog/dialog_mod'

    # 组件初始化执行
    mounted: () ->
      self = @

      self.setCurNavStyle()
      self.getUserSession()